#!/usr/bin/python

""" This script can be used to execute a single command on all the nodes.
    For example exec.py nodes 'iwconfig ath0 channel 4'.
"""

from nodes import readNodes, forNodes
from sys import argv, exit

if(len(argv) < 3):
    print """usage: %s ssh|sftp nodefile command [opts]
    s = serial
    d = debug
    n = non-blocking
    """ % argv[0]
    exit(-1)
    
program=str(argv[1])

nodes = readNodes(open(argv[2]))

cmd = argv[3]
opts = []
if(len(argv) > 4):
   opts = list(argv[4])


if('n' in opts and program == "ssh"):
    # don't ask me why it works
    cmd = "screen -D -m bash -c '%s' &" % cmd
run = program + ' %(usr)s@%(ip)s '


forNodes(nodes, run, cmd,serial=('s' in opts), debug=('d' in opts))

